package com.wideplay.crosstalk;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.wideplay.crosstalk.services.ServicesModule;
import com.wideplay.warp.hibernate.SessionPerRequestFilter;
import com.wideplay.warp.servlet.Servlets;
import com.wideplay.warp.servlet.WarpServletContextListener;
import com.wideplay.warp.widgets.Widgets;

/**
 * Called by web.xml on deploy of the web application. Creates & configs the injector for use
 *  by warp-widgets.
 *
 * @author Dhanji R. Prasanna (dhanji@gmail.com)
 */
public final class CrosstalkModule extends WarpServletContextListener {

    @Override
    protected Injector getInjector() {

        //bind in all of our service dependencies
        final Module services = new ServicesModule();


        //tell warp-widgets to scan this package
        final Module warpWidgets = Widgets
                .configure()
                .with(CrosstalkModule.class.getPackage())
                .buildModule();


        //map all incoming requests to Crosstalk's filter which acts as a bridge to warp-widgets
        final Module servlets = Servlets.configure()
                .filters()
                    .filter("/*").through(SessionPerRequestFilter.class)
                    .filter("/*").through(CrosstalkLifecycleFilter.class)

                .buildModule();

        
        //finally, create the injector with all our configuration
        return Guice.createInjector(services, warpWidgets, servlets);
    }
}
